package com.iemes.controller.equip;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.iemes.annotation.SystemLog;
import com.iemes.controller.index.BaseController;
import com.iemes.entity.EquipChekcontentFormMap;
import com.iemes.entity.ResFormMap;
import com.iemes.entity.UserFormMap;
import com.iemes.mapper.equip.EquipChekcontentMapper;
import com.iemes.plugin.PageView;
import com.iemes.util.Common;

@RequestMapping("equip")
@Controller
public class HoisChekcontentController extends BaseController{
	@Inject
	private EquipChekcontentMapper chekcontent;
			
	@RequestMapping(value = "hoist_content", method = RequestMethod.GET, produces = "text/html; charset=utf-8")
	public String examinepru(Model model) {
		List<ResFormMap> findByRes = findByRes();
		model.addAttribute("res", findByRes);
		return Common.BACKGROUND_PATH + "/equip/chekcontent/hois/equipbom_list";	
	}
	@ResponseBody
	@RequestMapping("equip_hoischeckByPage")
	public PageView findByPage(String pageNow, String pageSize) {
		EquipChekcontentFormMap equipChekcontentFormMap = getFormMap(EquipChekcontentFormMap.class);
		equipChekcontentFormMap.put("formCategory", 1);	
		if (equipChekcontentFormMap.get("status")==null) {
			equipChekcontentFormMap.put("status", 1);
		}
		List<Map<String,Object>> equipChekc = chekcontent.getEquipChekc(equipChekcontentFormMap);
		try {
			LuogicPaging(pageNow, pageSize, equipChekc);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return pageView;
	}

	
	@RequestMapping("equip_hoischeckAdd")
	public String addUI(Model model) throws Exception {
		return Common.BACKGROUND_PATH + "/equip/chekcontent/hois/equipbom_add";
	}

	@ResponseBody
	@RequestMapping("equip_hoischeckaddEntity")
	// @Transactional(readOnly=false)//需要事务操作必须加入此注解
	@SystemLog(module = "设备电葫芦日常保养点检内容表", methods = "设备-新增点检内容表")
	// 凡需要处理业务逻辑的.都需要记录操作日志
	public String addEntity() throws Exception {
		EquipChekcontentFormMap equipChekcontentFormMap = getFormMap(EquipChekcontentFormMap.class);
		if (equipChekcontentFormMap == null || equipChekcontentFormMap.size() == 0) {
			return "false";
		}
		// 获取request
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
				.getRequestAttributes()).getRequest();
		// 通过工具类获取当前登录的bean
		UserFormMap userFormMap = (UserFormMap) Common.findUserSession(request);
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date=new Date();
		equipChekcontentFormMap.put("date", sdf.format(date));
		equipChekcontentFormMap.set("status", 1);
		equipChekcontentFormMap.set("formCategory", 1);
		equipChekcontentFormMap.set("date", date);
		equipChekcontentFormMap.set("fillPeopleId", userFormMap.get("id"));
		chekcontent.addEntity(equipChekcontentFormMap);
		return "success";
	}
	
	
	@RequestMapping("hoisCheck_edit")
	public String editUI(Model model) throws Exception {
		String id = getPara("id");
		EquipChekcontentFormMap findbyFrist = chekcontent.findbyFrist("id", id,
				EquipChekcontentFormMap.class);
		model.addAttribute("equipAll", findbyFrist);
		return Common.BACKGROUND_PATH + "/equip/chekcontent/hois/equipbom_edit";
	}

	@ResponseBody
	@RequestMapping(value = "hoisCheck_editEntity")
	@SystemLog(module = "设备电葫芦日常保养点检内容表", methods = "设备-修改点检内容表")
	// 凡需要处理业务逻辑的.都需要记录操作日志
	public String editEntity() throws Exception {
		EquipChekcontentFormMap equipChekcontentFormMap = getFormMap(EquipChekcontentFormMap.class);
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
		// 通过工具类获取当前登录的bean
		UserFormMap userFormMap = (UserFormMap) Common.findUserSession(request);
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date=new Date();
		equipChekcontentFormMap.put("date", sdf.format(date));
		equipChekcontentFormMap.put("fillPeopleId", userFormMap.get("id"));
		chekcontent.editEntity(equipChekcontentFormMap);
		return "success";
	}
	
	@ResponseBody
	@RequestMapping(value = "hoisCheck_deleteEntity")
	@SystemLog(module = "设备电葫芦日常保养点检内容表", methods = "设备-删除保养点检内容表")
	// 凡需要处理业务逻辑的.都需要记录操作日志
	public String deleteEntity() throws Exception {
		EquipChekcontentFormMap equipChekcontentFormMap = getFormMap(EquipChekcontentFormMap.class);
		String[] ids = getParaValues("ids");
		String arr = ids[0];
		String[] str = arr.split(",");
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date=new Date();
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
				.getRequestAttributes()).getRequest();
		// 通过工具类获取当前登录的bean
		UserFormMap userFormMap = (UserFormMap) Common.findUserSession(request);
		for (int i = 0; i < str.length; i++) {
			equipChekcontentFormMap.put("fillPeopleId", userFormMap.get("id"));
			equipChekcontentFormMap.put("date", sdf.format(date));
			equipChekcontentFormMap.set("status", 2);
			equipChekcontentFormMap.put("id", str[i]);
			chekcontent.editEntity(equipChekcontentFormMap);
		}
		return "success";
	}
}
